package com.softcits.email.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.LocaleResolver;

import com.softcits.email.model.MailInfo;

@RestController
public class WelcomeController {
    
    @Autowired
    private MessageSource messageSource;
    
    @Autowired
    private LocaleResolver locale;
    
    @Value("${mail.ServerHost}")
    private String mailServerHost;

    @Value("${mail.SenderAddress}")
    private String mailSenderAddress;

    @Value("${mail.SenderNick}")
    private String mailSenderNick;

    @Value("${mail.SenderUsername}")
    private String mailSenderUsername;

    @Value("${mail.SenderPassword}")
    private String mailSenderPassword;
    
    private String name;

    @RequestMapping(value="/welcome/{id}", method=RequestMethod.GET)
    public MailInfo welcome(@PathVariable(name="id") Integer id, String name, @RequestParam(name="age", required=false) Integer age2) throws Exception {
        
        if(id != null && id ==2) {
            throw new Exception("manually throw exception.");
        }
        
        System.out.println("Id:" + id + " ; name:" + name + " ; age=" + age2);
        
        MailInfo info = new MailInfo();
        
        String[] toAddes = {
                "zhangsan@qq.com",
                "lisi@qq.com"
        };
        info.setToAddress(Arrays.asList(toAddes));
        info.setSubject("Test Subject");
        return info;
    }
    
    @RequestMapping(value="/data2", method=RequestMethod.GET)
    public Map<String, String> data2(ArrayList<String> book) {
        System.out.println(book);
        Map<String, String> map = new HashMap<>();
        
        map.put("java", "good book");
        
        return map;
    }
    
    @RequestMapping(value="/data4", method=RequestMethod.POST)
    public List<String> data4(@RequestBody String json) {
        System.out.println(json);
        
       
        
        return null;
    }
    
    
    @RequestMapping(value="/data3", method=RequestMethod.GET)
    public String data3(MailInfo info) {
     
        System.out.println(info.getSubject());
        return "hello";
    }
    
    @RequestMapping(value="/post", method=RequestMethod.POST)

    public String testPost(@RequestBody MailInfo info) {
        
        return new Date() + " " + info.getContent();
    }
    
    @RequestMapping(value="/put", method=RequestMethod.PUT)
    public Date testPut() {
  
        return new Date();
    }
    
    @RequestMapping(value="/delete", method=RequestMethod.DELETE)
    public Date testDelete() {
  
        return new Date();
    }
    
    @RequestMapping(value="/req")
    public String getRequestAndResponse(HttpServletRequest req, HttpServletResponse res) {
        
        System.out.println(req.getParameter("name"));
        
        return "ok";
    }
    
    @RequestMapping(value="/conv")
    public MailInfo getMailInfo() {
        
        MailInfo info = new MailInfo();
        
        String[] toAddes = {
                "zhangsan@qq.com",
                "lisi@qq.com"
        };
        info.setToAddress(Arrays.asList(toAddes));
        info.setSubject("Test Subject");
        info.setContent("this is a content");
        info.setSendingTime(new Date());
        info.setAttachmentSize(999999993.3);
        info.setManualFlag(true);
        return info;
        
    }
    
    @RequestMapping(value="/combine", method=RequestMethod.POST)
    public MailInfo combineMailInfo(@Valid @RequestBody MailInfo info, BindingResult result, HttpServletRequest request) throws Exception{
        if(result.hasErrors()) {
            throw new Exception("Validation is not pass" + this.messageSource.getMessage("label.login", null, locale.resolveLocale(request)));//result.getAllErrors());//this.messageSource.getMessage("msg.cc.null", null, Locale.ENGLISH));
        }
        return info;
        
    }
}
